<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="wilmar" uri="/wilmar-process"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>authorization</title>
	
	<script type="text/javascript">
		var i = 0;
		function createRows(){
				var table = document.getElementById("table2");
				var row = table.insertRow(1);
				row.id = i;
				var tRows = table.rows;
				var rCells = row.cells;
				var rCell_1 = tRows[row.rowIndex].insertCell(rCells.length);
				var rCell_2 = tRows[row.rowIndex].insertCell(rCells.length);
				var rCell_3 = tRows[row.rowIndex].insertCell(rCells.length);
				var rCell_4 = tRows[row.rowIndex].insertCell(rCells.length);

				rCell_1.innerHTML = "<input type='text' id='orgValue' name=processAuthorityList[" + i + "].organizationName readonly='true'></input><input type='hidden' name=processAuthorityList[" + i + "].organizationCode id='orgKey' readonly='true'></input><input type='hidden' name=processAuthorityList[" + i + "].processDefinitionKey id='proDefKey' readonly='true'></input>";
				rCell_2.innerHTML = "<input type='text' id='roleValue' name=processAuthorityList[" + i + "].roleName readonly='true'></input><input type='hidden' name=processAuthorityList[" + i + "].roleCode id='roleKey' readonly='true'></input>";
				rCell_3.innerHTML = "<input type='text' id='authorityTypeValue' readonly='true'></input><input type='hidden' name=processAuthorityList[" + i + "].authorizeType id='authorityTypeKey' readonly='true'></input>";
				rCell_4.innerHTML = "<input type='button' name=" + i + " onclick='delRow(this);' value='撤消'></input>";

				setValue();
				
				i++;
			}

		function delRow(button){
				// i--; 可能产生重复
				var row = document.getElementById(button.name);	
				var table = document.getElementById("table2");
				table.deleteRow(row.rowIndex);
			}


		function setValue(){
				// 获得选中项
				var authoritySelected = document.getElementById("authoritySelected");
	
				var aSelectedIndex = authoritySelected.selectedIndex;
				var aSelectedKey = authoritySelected.options[aSelectedIndex].value;
				var aSelectedValue = authoritySelected.options[aSelectedIndex].text;
	
				var orgSelected = document.getElementById("orgSelected");
	
				var oSelectedIndex = orgSelected.selectedIndex;
				var oSelectedKey = orgSelected.options[oSelectedIndex].value;
				var oSelectedValue = orgSelected.options[oSelectedIndex].text;
				
				var roleSelected = document.getElementById("roleSelected");
	
				var rSelectedIndex = roleSelected.selectedIndex;
				var rSelectedKey = roleSelected.options[rSelectedIndex].value;
				var rSelectedValue = roleSelected.options[rSelectedIndex].text;
	
				// 赋值 
				
				var proDefKey = document.getElementById("proDefKey");
				var proceDefKeyHidden = document.getElementById("proceDefKeyHidden");
				proDefKey.value = proceDefKeyHidden.value; 
				
				var orgValue = document.getElementById("orgValue");
				var orgKey = document.getElementById("orgKey");
				
				var roleValue = document.getElementById("roleValue");
				var roleKey = document.getElementById("roleKey");
	
				var authorityTypeValue = document.getElementById("authorityTypeValue");
				var authorityTypeKey = document.getElementById("authorityTypeKey");


				if(aSelectedKey == "PROCESS_SYSTEM_DEFINITION_AUTHORITY_TYPE_ORGANIZATION"){
						orgValue.value = oSelectedValue;
						orgKey.value = oSelectedKey;
						roleValue.value = "";
						roleKey.value = "";
					}
				else if(aSelectedKey == "PROCESS_SYSTEM_DEFINITION_AUTHORITY_TYPE_ROLE"){
						roleValue.value = rSelectedValue;
						roleKey.value = rSelectedKey;
						orgValue.value = "";
						orgKey.value = "";
					}
				else if(aSelectedKey == "PROCESS_SYSTEM_DEFINITION_AUTHORITY_TYPE_BOTH"){
						orgValue.value = oSelectedValue;
						orgKey.value = oSelectedKey;
						
						roleValue.value = rSelectedValue;
						roleKey.value = rSelectedKey;
					}
				
				authorityTypeValue.value = aSelectedValue;
				authorityTypeKey.value = aSelectedKey;
		alert('org ' + orgValue.value + '   ' + orgKey.value + '\n role ' + roleValue.value + '    ' + roleKey.value );	
			
			}
		
		function change(selected){
 	
				var index = selected.selectedIndex;
				var selectedValue = selected.options[index].value;
			//	alert('selected index: ' + index + '\nselected value: ' + selectedValue
			//			+ '\nselected text: ' + selected.options[selected.selectedIndex].text);
				var orgDiv = document.getElementById("orgDiv");
				var roleDiv = document.getElementById("roleDiv");
				switch(index){
					case 0 : 
					//	var orgDiv = document.getElementById("orgDiv");
						orgDiv.style.display="block";
						if(roleDiv.style.display=="block"){
							roleDiv.style.display="none";
							}
						break;
					case 1 :
					//	var roleDiv = document.getElementById("roleDiv");
						roleDiv.style.display="block";
						if(orgDiv.style.display=="block"){
							orgDiv.style.display="none";
							}
						break;
					case 2:
						orgDiv.style.display="block"; 
						roleDiv.style.display="block";
						break;
				}
			}


		
	</script>

</head>
<body>
<center><wilmar:process.logo /></center>
	<table id="table1" align="left"  cellspacing="0" cellpadding="0" border="0">
		<tr style="display: block">
			<td><s:text name="page.title.authorization.type" />:</td>
			<td><s:select id="authoritySelected" onchange="change(this);" list="#{'PROCESS_SYSTEM_DEFINITION_AUTHORITY_TYPE_ORGANIZATION':'对机构授权', 'PROCESS_SYSTEM_DEFINITION_AUTHORITY_TYPE_ROLE':'对角色授权', 'PROCESS_SYSTEM_DEFINITION_AUTHORITY_TYPE_BOTH':'对两者授权'}"></s:select></td>
		</tr>
		<tr style="display: block" id="orgDiv">
		<td><s:text name="page.title.authorization.type.org" />:</td>
		<td><s:select id="orgSelected" list="#{'oa':'丰益(上海)信息技术有限公司','ob':'益海嘉里集团','oc':'益海嘉里投资公司','od':'南海油脂工业(赤湾)有限公司'}"></s:select></td>
		</tr>
		<tr style="display: none" id="roleDiv">
			<td><s:text name="page.title.authorization.type.role" />:</td>
			<td><s:select id="roleSelected" list="#{'ra':'r0','rb':'r1','rc':'r2','rd':'r3'}"></s:select></td>
		</tr>
		<tr>
			<td colspan="2" align="left"><input type="button" value="add" onClick="createRows();"></input></td>
		</tr>
		
	</table>
	<br/>
	<br/>
	<br/>
	<br/>
	<br/>
	

	<s:form namespace="/process" action="processDefinitionAction!doAuthorition.action" method="post" >
	<s:hidden value="%{processAuthority.processDefinitionKey}" id="proceDefKeyHidden"></s:hidden>
	<table id="table2" width="50%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#f9f9f9" >
		<tr id="more"><td>机构</td><td>角色</td><td>授权类型</td><td>操作</td></tr>
		<tr><td colspan="4"><s:submit></s:submit></td></tr>
	</table>
	</s:form>
	
</body>
</html>